package org.eclipseuseradmin.internal.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;

public class NewUserWizardRegistry {

	private static final String NEW_USER_WIZARDS_EXTENSION_POINT = "newUserWizards";

	private List<WizardElement> wizardElements;

	public WizardElement[] getWizards() {

		if (wizardElements == null) {
			wizardElements = new ArrayList<WizardElement>();
			parseRegistry();
		}

		return wizardElements.toArray(new WizardElement[wizardElements.size()]);
	}

	protected void parseRegistry() {

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry.getExtensionPoint(
				UserAdminUiPlugin.PLUGIN_ID, NEW_USER_WIZARDS_EXTENSION_POINT);
		if (point == null) {
			return;
		}

		IExtension[] extensions = point.getExtensions();
		for (IExtension extension : extensions) {
			IConfigurationElement[] elements = extension
					.getConfigurationElements();
			for (IConfigurationElement element : elements) {

				wizardElements.add(getWizardElement(element));
			}
		}
	}

	private WizardElement getWizardElement(IConfigurationElement element) {

		return new WizardElement(element);
	}

}
